package gr.data.dao;

import gr.data.hbn.pojo.TypeAddress;

import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

import test.hbn.PopolaUser;

/**
 * A data access object (DAO) providing persistence and search support for
 * TypeAddress entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see gr.data.hbn.pojo.TypeAddress
 * @author MyEclipse Persistence Tools
 */

public class TypeAddressDAO extends BaseHibernateDAO {
	private static final Logger log= Logger.getLogger(TypeAddressDAO.class);
	// property constants
	public static final String DESCRIZIONE = "descrizione";

	public void save(TypeAddress transientInstance) {
		log.debug("saving TypeAddress instance");
		try {
			Transaction tx = beginTransaction();
			getSession().save(transientInstance);
			tx.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(TypeAddress persistentInstance) {
		log.debug("deleting TypeAddress instance");
		try {
			Transaction tx = beginTransaction();
			getSession().delete(persistentInstance);
			tx.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public TypeAddress findById(java.lang.Integer id) {
		log.debug("getting TypeAddress instance with id: " + id);
		try {
			TypeAddress instance = (TypeAddress) getSession().get(
					"gr.data.hbn.xml.TypeAddress", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(TypeAddress instance) {
		log.debug("finding TypeAddress instance by example");
		try {
			List results = getSession().createCriteria(
					"gr.data.hbn.xml.TypeAddress")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TypeAddress instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TypeAddress as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByDescrizione(Object descrizione) {
		return findByProperty(DESCRIZIONE, descrizione);
	}

	public List findAll() {
		log.debug("finding all TypeAddress instances");
		try {
			String queryString = "from TypeAddress";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public TypeAddress merge(TypeAddress detachedInstance) {
		log.debug("merging TypeAddress instance");
		try {
			TypeAddress result = (TypeAddress) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(TypeAddress instance) {
		log.debug("attaching dirty TypeAddress instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(TypeAddress instance) {
		log.debug("attaching clean TypeAddress instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}